// pages/profile/profile.js
const util = require('../../utils/util')
const app = getApp()

Page({
  data: {
    userInfo: null,
    hasUserInfo: false,
    statistics: {
      totalRecords: 0,
      totalAmount: 0,
      thisMonthRecords: 0,
      thisMonthAmount: 0
    },
    menuItems: [
      {
        icon: '/images/record-icon.png',
        title: '我的记录',
        desc: '查看所有用车记录',
        url: '/pages/record/record'
      },
      {
        icon: '/images/create-icon.png',
        title: '创建记录',
        desc: '添加新的用车记录',
        url: '/pages/create-record/create-record'
      },
      {
        icon: '/images/driver-icon.png',
        title: '司机信息',
        desc: '查看司机列表',
        url: '/pages/driver-list/driver-list'
      },
      {
        icon: '/images/settings-icon.png',
        title: '设置',
        desc: '应用设置和偏好',
        url: '/pages/settings/settings'
      }
    ]
  },

  onLoad() {
    this.checkUserInfo()
  },

  onShow() {
    this.checkUserInfo()
    this.loadStatistics()
  },

  // 检查用户信息
  checkUserInfo() {
    if (app.globalData.userInfo) {
      this.setData({
        hasUserInfo: true,
        userInfo: app.globalData.userInfo
      })
    } else {
      this.setData({
        hasUserInfo: false,
        userInfo: null
      })
    }
  },

  // 加载统计数据
  async loadStatistics() {
    try {
      const res = await util.request('/car-records/statistics')
      
      if (res.code === 200) {
        this.setData({
          statistics: res.data
        })
      }
    } catch (error) {
      console.error('加载统计数据失败:', error)
    }
  },

  // 登录
  login() {
    wx.navigateTo({
      url: '/pages/login/login'
    })
  },

  // 退出登录
  async logout() {
    const confirm = await util.showModal('确认退出', '确定要退出登录吗？')
    if (!confirm) return
    
    try {
      // 清除本地存储
      wx.removeStorageSync('token')
      
      // 清除全局数据
      app.globalData.token = ''
      app.globalData.userInfo = null
      
      // 更新页面状态
      this.setData({
        hasUserInfo: false,
        userInfo: null,
        statistics: {
          totalRecords: 0,
          totalAmount: 0,
          thisMonthRecords: 0,
          thisMonthAmount: 0
        }
      })
      
      util.showToast('已退出登录', 'success')
    } catch (error) {
      console.error('退出登录失败:', error)
      util.showToast('退出失败')
    }
  },

  // 菜单项点击
  onMenuTap(e) {
    const url = e.currentTarget.dataset.url
    
    if (!this.data.hasUserInfo) {
      util.showToast('请先登录')
      this.login()
      return
    }
    
    if (url.startsWith('/pages/record/record') || url.startsWith('/pages/create-record/create-record')) {
      wx.switchTab({ url })
    } else {
      wx.navigateTo({ url })
    }
  },

  // 查看统计详情
  viewStatistics() {
    if (!this.data.hasUserInfo) {
      util.showToast('请先登录')
      this.login()
      return
    }
    
    wx.navigateTo({
      url: '/pages/statistics/statistics'
    })
  },

  // 分享应用
  onShareAppMessage() {
    return {
      title: '调车系统 - 便捷的用车记录管理',
      path: '/pages/index/index',
      imageUrl: '/images/share-image.png'
    }
  },

  // 格式化金额
  formatAmount(amount) {
    return parseFloat(amount || 0).toFixed(2)
  }
})